package org.agile2soft.nexoma.tapestry.mixins;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.RenderSupport;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.ioc.annotations.Inject;

@IncludeJavaScriptLibrary("PopupPageLink.js")
public class PopupPageLink {
    @Inject
    private ComponentResources resources;
    @Environmental
    private RenderSupport renderSupport;
    @InjectContainer
    private ClientElement container;
    @Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
    private String page;
    @Parameter(defaultPrefix = BindingConstants.LITERAL, value = "800")
    private String width;
    @Parameter(defaultPrefix = BindingConstants.LITERAL, value = "600")
    private String height;
    @Parameter
    private Object[] context;

    void afterRender() {
        Link link = resources.createPageLink(page, true, context);
        renderSupport.addScript("new PopupPageLink('%s', '%s', %s, %s);",
                container.getClientId(), link, width, height);
    }
}